package com.s7.compat.adapter;

import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.s7.compat.listener.OnRadioTabListener;

import java.util.List;


public class FragmentPagerTabAdapter implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {

    private FragmentManager mManager;
    private ViewPager mPager;
    private RadioGroup rgs;
    private List<Fragment> fragments;
    private BaseFragmentPagerAdapter mAdapter;
    private OnRadioTabListener mListener;

    private int currentTab = 0;


    public FragmentPagerTabAdapter(FragmentManager mManager, ViewPager mPager, RadioGroup rgs,
                                   List<Fragment> fragments) {
        this.mManager = mManager;
        this.mPager = mPager;
        this.rgs = rgs;
        this.fragments = fragments;
        init();
    }

    public FragmentPagerTabAdapter(FragmentManager mManager, ViewPager mPager, RadioGroup rgs,
                                   List<Fragment> fragments, OnRadioTabListener mListener) {
        this.mManager = mManager;
        this.mPager = mPager;
        this.rgs = rgs;
        this.fragments = fragments;
        this.mListener = mListener;
        init();
    }

    public FragmentPagerTabAdapter(FragmentManager mManager, ViewPager mPager, RadioGroup rgs,
                                   List<Fragment> fragments, int currentTab) {
        this.mManager = mManager;
        this.mPager = mPager;
        this.rgs = rgs;
        this.fragments = fragments;
        this.currentTab = currentTab;
        init();
    }

    public FragmentPagerTabAdapter(FragmentManager mManager, ViewPager mPager, RadioGroup rgs,
                                   List<Fragment> fragments, int currentTab, OnRadioTabListener mListener) {
        this.mManager = mManager;
        this.mPager = mPager;
        this.rgs = rgs;
        this.fragments = fragments;
        this.currentTab = currentTab;
        this.mListener = mListener;
        init();
    }

    private void init() {
        mAdapter = new BaseFragmentPagerAdapter(mManager, this.fragments);
        mPager.setAdapter(mAdapter);
        setCurrentTab(currentTab);
        mPager.addOnPageChangeListener(this);
        rgs.setOnCheckedChangeListener(this);
    }

    public void setCurrentTab(int currentTab) {
        onShowRadio(currentTab);
        mPager.setCurrentItem(currentTab, false);
    }

    private void onShowRadio(int index) {
        for(int i = 0; i < rgs.getChildCount(); i++){
            if (i == index) {
                if (mListener != null) {
                    if (mListener.onRgsTab(rgs, rgs.getChildAt(i).getId())) {
                        rgs.check(rgs.getChildAt(i).getId());
                    } else {
                        setCurrentTab(currentTab);
                    }
                } else {
                    rgs.check(rgs.getChildAt(i).getId());
                }
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        onShowRadio(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (mListener != null) {
            if (mListener.onRgsTab(group, checkedId)) {
                for(int i = 0; i < rgs.getChildCount(); i++){
                    if(rgs.getChildAt(i).getId() == checkedId){
                        mPager.setCurrentItem(i,false);
                    }
                }
            } else {
                setCurrentTab(0);
            }
        } else {
            for(int i = 0; i < rgs.getChildCount(); i++){
                if(rgs.getChildAt(i).getId() == checkedId){
                    mPager.setCurrentItem(i,false);
                }
            }
        }
    }
}
